package cn.fancy.sdk;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import cn.fancy.sdk.common.utils.Logger;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.entity.GuildBaseInfo;
import cn.fancy.sdk.login.QdLoginCallback;
import cn.fancy.sdk.login.QdLoginResult;
import cn.fancy.sdk.pay.QdPayCallback;
import cn.fancy.sdk.pay.entity.PayCallbackInfo;
import com.tencent.mm.opensdk.modelbiz.WXLaunchMiniProgram;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

public class FancyDemo extends Activity implements OnClickListener {
    
    private LinearLayout payLL;
    private Button testLoginModuleBtn, testQuestionBtn, testOpenWebviewBtn,
        paymentBtnOne, paymentBtnTwo, bindPhoneBtn, logout;
    private TextView msg;
    
    public Handler uiHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if(msg.what == 0){
                showlogin();
            }
            return false;
        }
    });
    
    public void showlogin() {
        testLoginModuleBtn.setVisibility(View.VISIBLE);
        payLL.setVisibility(View.GONE);
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ResUtil.getLayoutId(getBaseContext(), "guild_demo"));
    

        String yourLoginKey = GuildBaseInfo.getInstance().getMetaUrl(this, "DEMO_KEY"); //这里使用你自己申请的loginKey
        String test_game_id = GuildBaseInfo.getInstance().getMetaUrl(this, "TEST_GAME_ID");
        Log.d("qdsdkdemo", "yourLoginKey:" + yourLoginKey + ", test_game_id:" + test_game_id);
    
        initSDK(test_game_id, yourLoginKey);
    
        
        //FancyManager.getInstance().setVerticalOrientation();
    
        // 设置退出监听对象
        FancyManager.getInstance().setLogoutListen(uiHandler, getBaseContext());
        
        msg = (TextView) this.findViewById(ResUtil.getId(this.getBaseContext(),
            "DemoMsg"));
        
        testLoginModuleBtn = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "testLoginModuleBtn"));
        testLoginModuleBtn.setOnClickListener(this);
        testLoginModuleBtn.setVisibility(View.VISIBLE);
        testLoginModuleBtn.setText("登录");
        
        testOpenWebviewBtn = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "testOpenWebviewBtn"));
        testOpenWebviewBtn.setOnClickListener(this);
        testOpenWebviewBtn.setVisibility(View.VISIBLE);
        
        testQuestionBtn = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "TestQuestionModule"));
        testQuestionBtn.setOnClickListener(this);
        testQuestionBtn.setVisibility(View.VISIBLE);
        
        payLL = (LinearLayout) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "guild_demo_pay"));
        payLL.setVisibility(View.GONE);
        
        paymentBtnOne = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "paymentBtnOne"));
        paymentBtnOne.setOnClickListener(this);
        
        paymentBtnTwo = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "paymentBtnTwo"));
        paymentBtnTwo.setOnClickListener(this);
        paymentBtnTwo.setVisibility(View.GONE);
        
        logout = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "user_logout"));
        logout.setOnClickListener(this);
        
        bindPhoneBtn = (Button) this.findViewById(ResUtil.getId(
            this.getBaseContext(), "bindMBtn"));
        bindPhoneBtn.setOnClickListener(this);
        
        findViewById(ResUtil.getId(this.getBaseContext(), "upload_create")).setOnClickListener(this);
        findViewById(ResUtil.getId(this.getBaseContext(), "upload_entergame")).setOnClickListener(this);
        findViewById(ResUtil.getId(this.getBaseContext(), "upload_levelup")).setOnClickListener(this);
    }
    
    private void initSDK(String test_game_id, String yourLoginKey) {
        FancyManager.getInstance().init(this, test_game_id, yourLoginKey, "561139", new QdRegisterCallback() {
        
            @Override
            public void callback(int resultCode, String method) {
            
            }
        });
    }
    
    @Override
    public void onBackPressed() {
        new AlertDialog.Builder(this).setMessage("确定退出？")
            .setOnCancelListener(new OnCancelListener() {
                
                @Override
                public void onCancel(DialogInterface dialog) {
                
                }
            })
            
            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    FancyDemo.this.finish();
                }
            })
            
            .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                
                @Override
                public void onClick(DialogInterface dialog, int which) {
                
                }
            }).show();
    }
    
    @Override
    public void onClick(View v) {
        
        int vid = v.getId();
        
        if (vid == ResUtil.getId(this.getBaseContext(), "testLoginModuleBtn")) {
            FancyManager.getInstance().login(FancyDemo.this, new QdLoginCallback() {
                
                @Override
                public void callback(int resultCode,
                    String resultDescription,
                    QdLoginResult loginResult) {
                    
                    if (resultCode == QdLoginCallback.SUCCEEDED) {
                        /* 用户登录成功，提取loginResult中的数据，进行之后的操作 */
                        
                        testLoginModuleBtn.setVisibility(View.GONE);
                        msg.setVisibility(View.GONE);
                        payLL.setVisibility(View.VISIBLE);
                        
                        Bundle bundle = new Bundle();
                        bundle.putString(FancyManager.SERVER_ID, "2");// 服务器id
                        bundle.putString(FancyManager.SERVER_NAME, "123");// 服务器名
                        bundle.putString(FancyManager.ROLE_ID, "12321");// 角色id
                        bundle.putString(FancyManager.ROLE_NAME, "sad&day\\me '< > # @ ");
                        bundle.putString(FancyManager.ROLE_LEVEL, "1");
                        FancyManager.getInstance().rolestatis(FancyDemo.this, bundle,
                            RoleStatis.AC_CREATE_ROLE);
                        
                        Log.d("qdsdkdemo", loginResult.userId);
                        
                    } else if (resultCode == QdLoginCallback.USER_QUIT) {
                        Logger.e("guild_sdk_login_user_quit",
                            resultDescription);
                        
                        showToastMsg("Demo: 退出登录模块");
                        /* 处理用户退出登录模块 */
                    }
                }
            });
        } else if (vid == ResUtil.getId(this.getBaseContext(), "paymentBtnOne")) {
            if (vid == ResUtil.getId(this.getBaseContext(), "paymentBtnOne")) {
                FancyManager.getInstance().pay(getBaseContext(), "0", 1, "元宝",
                    "DM00123C0109060815997", "透传ext字段",
                    new QdPayCallback() {
                        
                        @Override
                        public void onPayFinish(PayCallbackInfo callback) {
                            Toast.makeText(FancyDemo.this,
                                callback.toString(), Toast.LENGTH_SHORT)
                                .show();
                        }
                    });
            }
        } else if (vid == ResUtil.getId(this.getBaseContext(), "paymentBtnTwo")) {
            // CmgeSdkManager.getInstance().pay(CmgeSdkDemo.this, "1010",
            // " ",
            // "1010", "01", "", 0, "SDK充值测试", "自定义参数。。。。", 0,0,
            // new CmgePayListener() {
            // @Override
            // public void onPayFinish(PayCallbackInfo callback) {
            // Toast.makeText(CmgeSdkDemo.this,
            // callback.toString(), Toast.LENGTH_SHORT)
            // .show();
            // }
            // });
            
        } else if (vid == ResUtil.getId(this.getBaseContext(), "user_logout")) {
            Log.e("logout", "logout logout");
            FancyManager.getInstance().showPermissionActivity(FancyDemo.this);
            
        } else if (vid == ResUtil.getId(this, "guild_fcmStats_btn")) { // 获取防沉迷状态
            String cmStatus = FancyManager.getInstance().getCmCertStatus();
            Toast.makeText(this, cmStatus, Toast.LENGTH_SHORT).show();
        } else if (vid == ResUtil.getId(this, "bindMBtn")) {
        
        } else if (vid == ResUtil.getId(this, "TestQuestionModule")) {
            // 联系客服按钮
            IWXAPI iwxapi = WXAPIFactory.createWXAPI(this, "wxb1c59b30147edab7", true);
            WXLaunchMiniProgram.Req wxReq = new WXLaunchMiniProgram.Req();
            wxReq.userName = "gh_c640297ec453";
            //wxReq.path = "";
            wxReq.miniprogramType = WXLaunchMiniProgram.Req.MINIPTOGRAM_TYPE_RELEASE;

            iwxapi.sendReq(wxReq);
            
        } else if (vid == ResUtil.getId(this, "testOpenWebviewBtn")) {
            Log.e("webservice", "webservice");
        } else if (vid == ResUtil.getId(this, "upload_create")) {
            Bundle bundle = new Bundle();
            bundle.putString(FancyManager.SERVER_ID, "2");// 服务器id
            bundle.putString(FancyManager.SERVER_NAME, "123");// 服务器名
            bundle.putString(FancyManager.ROLE_ID, "12321");// 角色id
            bundle.putString(FancyManager.ROLE_NAME, "sexy&day\\me '< > # @ ");
            bundle.putString(FancyManager.ROLE_LEVEL, "1");
            FancyManager.getInstance().rolestatis(FancyDemo.this, bundle, RoleStatis.AC_CREATE_ROLE);
        } else if (vid == ResUtil.getId(this, "upload_entergame")) {
            Bundle bundle = new Bundle();
            bundle.putString(FancyManager.SERVER_ID, "2");// 服务器id
            bundle.putString(FancyManager.SERVER_NAME, "123");// 服务器名
            bundle.putString(FancyManager.ROLE_ID, "12321");// 角色id
            bundle.putString(FancyManager.ROLE_NAME, "sexy&day\\me '< > # @ ");
            bundle.putString(FancyManager.ROLE_LEVEL, "1");
            FancyManager.getInstance().rolestatis(FancyDemo.this, bundle, RoleStatis.AC_ENTER_GAME);
        } else if (vid == ResUtil.getId(this, "upload_levelup")) {
            Bundle bundle = new Bundle();
            bundle.putString(FancyManager.SERVER_ID, "2");// 服务器id
            bundle.putString(FancyManager.SERVER_NAME, "123");// 服务器名
            bundle.putString(FancyManager.ROLE_ID, "12321");// 角色id
            bundle.putString(FancyManager.ROLE_NAME, "sexy&day\\me '< > # @ ");
            bundle.putString(FancyManager.ROLE_LEVEL, "2");
            FancyManager.getInstance().rolestatis(FancyDemo.this, bundle, RoleStatis.AC_ROLE_LEVEL_UP);
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        Log.e("qdsdkdemoresume", "qdsdkdemoresume");
        FancyManager.getInstance().onResume(this);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        FancyManager.getInstance().onPause(this);
        Log.e("qdsdkdemo_pause", "qdsdkdemo_pause");
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        Log.e("qdsdkdemo_restart", "qdsdkdemo_restart");
    }
    
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e("qdsdkdemo_destroy", "qdsdkdemo_destroy");
        FancyManager.getInstance().Ondestroy();
    }
    
    public void showToastMsg(final String msg) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(FancyDemo.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
    }
    
}
